/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
package com.joolun.web.api;

import com.joolun.common.core.domain.AjaxResult;
import com.joolun.mall.entity.Chain;
import com.joolun.mall.entity.ChainOrder;
import com.joolun.mall.entity.ChainPlan;
import com.joolun.mall.service.IChainPlanService;
import com.joolun.weixin.utils.ThirdSessionHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 * 接龙计划列表
 * @author JL
 * @date 2019-09-10 15:21:22
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/weixin/api/ma/chainPlan")
@Api(value = "plan", tags = "接龙计划api")
public class PlanApi {

	@Autowired
	private IChainPlanService planService;


	/**
	 * 接龙计划--详情
	 * @param planId 接龙计划id
	 * @return AjaxResult
	 */
	@ApiOperation(value = "接龙计划详情")
	@GetMapping("detail/{planId}")
	public AjaxResult<ChainPlan> getById(@PathVariable("planId") Long planId){
		ChainPlan detail = planService.detail(planId);
		String userId = ThirdSessionHolder.getThirdSession().getWxUserId();
		List<Long> userChainIds = planService.userChains(planId, userId);

		detail.getChains().forEach(chain -> {
			chain.setIsAlready(userChainIds.contains(chain.getId()));
		});

		//排序按照beginDate升序
		if(Objects.nonNull(detail.getChains())){
			List<Chain> chains = detail.getChains().stream().sorted(Comparator.comparing(Chain::getBeginDate))
					.collect(Collectors.toList());;
			detail.setChains(chains);
		}

		return AjaxResult.success(detail);

	}

//	/**
//	 * 根据计划id返回用户已经接龙的链接id
//	 * @param planId 接龙计划id
//	 * @return AjaxResult
//	 */
//	@ApiOperation(value = "获取接龙计划中已经接龙的链接id列表")
//	@GetMapping("userChains/{planId}")
//	public AjaxResult<List<Long>> userChains(@PathVariable("planId") Long planId){
//		String userId = ThirdSessionHolder.getThirdSession().getWxUserId();
//		return AjaxResult.success(planService.userChains(planId,userId));
//	}

}
